using UnityEngine;
using System.Collections;



public class Inventory : MonoBehaviour
{
    const int INVENTORY_SIZE = 6;
    // GUI
    Vector2 posBag = new Vector2(10, 10);
    public float slotSize = 30;
    public float spacingBetweenSlots = 3;
    public Texture2D defaultTextureSlot,tmpTexture;
    public Texture2D[] inventoryIconTextures ;
    public GUIStyle boxStyle;
    Texture mouseDragTexture; // this for drag & drop cursor
	
    // Data struct
    public int selectedItem = -1;
    bool[] flagAvailable; // marks which slot in the bag are available
    GameObject itemInHand; // mark which object is held in handg.
    GameObject[] itemsInBag; //represents all the objects in the bag
    bool itemPickedUpFlag = false; // track wether user has picked up a inventory item to place.
    public bool inventoryButtonClick = false; // used to track wether mouse was used to pick-up or drop items ; to notify other system not use the mouse 
    public InventoryMouseHandler mouseHandler;
    public Camera mycam;


    void Start()
    {
        itemsInBag = new GameObject[INVENTORY_SIZE];
        flagAvailable = new bool[INVENTORY_SIZE];
        inventoryIconTextures = new Texture2D[INVENTORY_SIZE];
        for (int x = 0; x < INVENTORY_SIZE; x++)
        {
            itemsInBag[x] = null;
            flagAvailable[x] = true;
            inventoryIconTextures[x] = defaultTextureSlot;
        } 
    }
    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
           // print("GetMouseButtonUp");
            if (!itemPickedUpFlag && selectedItem != -1)
            {     itemPickedUpFlag = true;
            }
            else if (itemInHand != null)
            {

                print("ReleaseObjectFromInventory");
                ReleaseObjectFromInventory(selectedItem);
                selectedItem = -1;
                itemInHand = null;
                itemPickedUpFlag = false;
                print("Update- inventoryButtonClick :" + inventoryButtonClick);
                inventoryButtonClick = false;
            }
        }
        //if (Input.GetKey(KeyCode.Escape) && IsItemPickedUp())
        //{
        //    print("dropping picked item");
        //    CancelPickedItem();
        //}

    }

    void OnGUI()
    {
        Bag(); 
    }

    

    void Bag( )
    {
        tmpTexture = defaultTextureSlot;
        for (int curIdx = 0; curIdx < INVENTORY_SIZE; curIdx++)
        {
            if (itemsInBag[curIdx] == null)
            {//there's no object in this slot, set the default texture
                tmpTexture = defaultTextureSlot;
            }
            else
            {
               tmpTexture = itemsInBag[curIdx].GetComponent<InventoryItem>().iconTexture;//set the object's texture	
            }
            inventoryIconTextures[curIdx] = tmpTexture;
        }
        float width = slotSize * itemsInBag.Length;
        
        //print(selectedItem); 
        selectedItem = GUI.SelectionGrid(new Rect(posBag.x, posBag.y, width, slotSize), selectedItem, inventoryIconTextures, INVENTORY_SIZE);
        if (selectedItem != -1 && itemsInBag[selectedItem] != null && itemPickedUpFlag == false)  
        {
            print(" you clicked on a inventory item");
            inventoryButtonClick = true;
            SetItemPickedUp(itemsInBag[selectedItem]);
        }
    }


    public void SaveObjectInInventory(GameObject prop)
    {
		//print (prop);
        for (int x = 0; x < INVENTORY_SIZE; x++)
        {
			//print(x);
            if (itemsInBag[x] == null)
            {
				
                itemsInBag[x] = prop;
                flagAvailable[x] = false;
                //print(prop.transform.localPosition);
                prop.transform.localPosition = Vector3.zero;
                prop.active = false;
                prop.transform.parent = transform; 

                break;
            }
        }
    }

	
	public bool IsItemPickedUp()
	{
		return itemPickedUpFlag;
	}
	

    public GameObject GetItemPickedUp()
    {
        return itemInHand;
    }

	public void SetItemPickedUp(GameObject itemObj)
	{
		if (itemObj)
		{
			//itemPickedUpFlag = true;
			itemInHand = itemObj;
            mouseHandler.SetCursor(itemObj.GetComponent<InventoryItem>().iconTexture);
			
		}
		else
		{
			//itemPickedUpFlag = false;
			itemInHand = null;
            mouseHandler.ReleaseCursorIcon();
		}

        return;
	}

    public bool CancelPickedItem() 
    {
        if (itemInHand)
		{
            selectedItem = -1;
			itemPickedUpFlag = false;
            itemInHand = null;
            mouseHandler.ReleaseCursorIcon();
			return true;
		}
        return false;
    }


    
    void ReleaseObjectFromInventory(int t)
    {//releases the object at index t from the inventory.

        itemsInBag[t].transform.position = mycam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -mycam.transform.position.z));
        print(itemsInBag[t].transform.position);
            //new Vector3(0.0f, 1.0f, 0.2f);
        itemsInBag[t].transform.parent = null;
		itemsInBag[t].active = true;
	    mouseHandler.ReleaseCursorIcon();
        itemsInBag[t] = null;
    }

}